from domain.domain import Paper
from service.Message import Message,CodeMessageData
import re

class PaperDAO():
    def __init__(self):
        print("PaperDAO init")

    def add(self, session, onePaper):
        """
        :param session: session
        :param onePaper: one Paper data
        :return: Message Object
        """
        data = []
        try:
            session.add(onePaper)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    # 一次提交多个数据
    def addAll(self, session, paperList):
        try:
            session.add_all(paperList)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])


    def deleteById(self, session, id):
        """
        :param session: session
        :param id: personalTeamid
        :return: result Message Object
        """
        data = []
        try:
            session.query(Paper).filter(Paper.id == id).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def deleteByJobNumberAndIdlist(self, session,  jobNumber, idList, adminFlag=0):
        """
              :param session: session
              :param id: personalTeamid
              :return: result Message Object
              """
        data = []
        try:
            # session.query(PersonalTeam).filter(PersonalTeam.id._in(id)).delete()
            if (adminFlag):
                for each in idList:
                    session.query(Paper).filter(Paper.id == each).delete()
            else:
                for each in idList:
                    session.query(Paper).filter(Paper.id == each).filter(Paper.job_number==jobNumber).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def getAllInfo(self, session,jobNumber,DHFlag=0):
        """
        get all info
        :param session: sql session
        :return:
        """
        allData = []
        try:
            if(DHFlag):
                list = session.query(Paper).all()
            else:
                list = session.query(Paper).filter(Paper.job_number==jobNumber).all()

            code = 0000
            for each in list:
               allData.append(each.toDict())
        except:
            code = 1009
        return Message(code, CodeMessageData[code], data=allData)

    def updateByJobNumberAndDataList(self, session, jobNumber, newData,adminFlag=0):
        code = 0000
        try:
            for one in newData:
                if(adminFlag):
                    part = session.query(Paper).filter(Paper.id == one.id).one()
                else:
                    part = session.query(Paper).filter(Paper.id == one.id).filter(Paper.job_number==jobNumber).one()
                part.id = one.id
                part.job_number = one.job_number
                part.result_name = one.result_name
                part.result_form = one.result_form
                part.publication_name = one.publication_name
                part.issuing_time = one.issuing_time
                part.isbn_number = one.isbn_number
                part.publication_level = one.publication_level
                part.search_info = one.search_info
                part.order = one.order
                part.total_people = one.total_people
            session.commit()
        except Exception as ex:
            code = 1017
            session.rollback()
        return Message(code, CodeMessageData[code])

    def getDataByJobNumberAndIdList(self, session, jobNumber, idList,adminFlag=0):
        code = 0000
        data=[]
        try:
            for one in idList:
                if(adminFlag):
                    part = session.query(Paper).filter(Paper.id == one).one()
                else:
                    part = session.query(Paper).filter(Paper.id == one).filter(Paper.job_number==jobNumber).one()
                data.append(part.toDict())
        except Exception as ex:
            data=[]
            code = 1028

        return Message(code, CodeMessageData[code],data)


    def getName(self,session,id):
        code=0000
        data=[]
        try:
            result=session.query(Paper).filter(Paper.id==id).one()
            data.append(result.result_name)
        except:
            code=1043
            data=[]
        return Message(code,CodeMessageData[code],data=data)